'this is a string'
'this is a string'
len
¶len('word and word')
13
You can use len
to get the length of a string.
'fire' + 'place'
'fireplace'
'yo' * 2
'yoyo'
'nan ' * 16 + 'batman!'
'nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan batman!'
+=
¶message = 'Hello'
message = message + ' world!'
message
'Hello world!'
message = 'Hello'
message += ' world!'
message
'Hello world!'
for letter in 'this is a string':
print(letter)
t h i s i s a s t r i n g
'a'.isalpha(), '8'.isalpha()
(True, False)
'abcdefg'.isalpha(), 'abc1234'.isalpha(), 'abc!'.isalpha()
(True, False, False)
'a'.isdigit(), '8'.isdigit()
(False, True)
'12345'.isdigit(), '12345pi'.isdigit(), '123.456'.isdigit()
(True, False, False)
'a'.isalnum(), '8'.isalnum()
(True, True)
'12345'.isalnum(), '12345pi'.isalnum(), '123.456'.isalnum()
(True, True, False)
'a'.isspace(), '8'.isspace(), ' '.isspace()
(False, False, True)
'A'.islower(), 'A'.isupper()
(False, True)
'a'.islower(), 'a'.isupper(), '9'.islower()
(True, False, False)
'a'.upper(), 'a'.lower()
('A', 'a')
'A'.upper(), 'A'.lower()
('A', 'a')
characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_+=[]{}"\'|:;,./?<> \t\n'
# isalpha
alphas = ''
for character in characters:
if character.isalpha():
alphas += character
print(alphas)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# isdigit
digits = ''
for character in characters:
if character.isdigit():
digits += character
print(digits)
0123456789
# isalnum
alphanumeric = ''
for character in characters:
if character.isalnum():
alphanumeric += character
print(alphanumeric)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
# isspace
spaces = ''
for character in characters:
if character.isspace():
spaces += character
print(spaces)
# isspace
spaces = []
for character in characters:
if character.isspace():
spaces.append(character)
print(spaces)
[' ', '\t', '\n']
# other stuff
symbols = ''
for character in characters:
if not character.isspace() and not character.isalnum():
symbols += character
print(symbols)
`~!@#$%^&*()-_+=[]{}"'|:;,./?<>
# upper and lower
uppers = ''
lowers = ''
for character in characters:
if character.isupper():
uppers += character
elif character.islower():
lowers += character
print(uppers)
print(lowers)
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
Write a function that replaces all space characters with dashes.
def no_spaces(text):
"""Replace all space characters with dashes"""
new_text = ''
for char in text:
if char.isspace():
char = '-'
new_text += char
return new_text
print(no_spaces('BYU is the place to be.'))
BYU-is-the-place-to-be.
message = """This is a long,
multiline string.
It has multiple lines.
That is what "multiline" means. :)"""
print(message)
print()
print(no_spaces(message))
This is a long, multiline string. It has multiple lines. That is what "multiline" means. :) This-is-a-long,-multiline-string.-It-has-multiple-lines.-That-is-what-"multiline"-means.-:)
print(no_spaces('Goodbye spaces \t tabs \n and newlines'))
Goodbye-spaces---tabs---and-newlines
Write a function that replaces every number in a string with ?
def no_numbers(text):
"""Replace every number with ?"""
new_text = ''
for char in text:
if char.isdigit():
char = '?'
new_text += char
return new_text
no_numbers('There were 7 people.')
'There were ? people.'
no_numbers('15 out of 25 have more than 17.3% contamination.')
'?? out of ?? have more than ??.?% contamination.'
no_numbers('2 + 2 = 5, for large values of 2.')
'? + ? = ?, for large values of ?.'
Add up all the digits found in a string.
def find_digits(text):
"""Return a list of the digits (as ints) from the text"""
digits = []
for c in text:
if c.isdigit():
digits.append(int(c))
return digits
def add_digits(text):
"""Add all the digits found in the `text`.
>>> add_digits('123foo')
6
"""
digits = find_digits(text)
return sum(digits)
add_digits('123foo')
6
add_digits('10 students ate 6 oranges and 42 students ate 7 pears.')
20
Write a program that "organizes" user input.
"Organized" text means that all the characters are reordered to this sequence:
organize.py
¶Text: Hello, what is your name?
ellowhatisyournameH,?
Text: BYU is my favorite school!
ismyfavoriteschoolBYU!
Text: 3.14159 is a loose approx. for PI.
isalooseapproxforPI314159...
Text:
'foo' + 'bar'
, 'BYU! ' * 5
.isalpha()
, .isdigit()
, .isalnum()
, .isspace()
, .isupper()
, .islower()
.upper()
, .lower()
+=